package regpang.myapplication22.ViscosityControl;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import regpang.myapplication22.R;
import regpang.myapplication22.ViscosityControl.utils.DisplayUtils;
import regpang.myapplication22.ViscosityControl.view.StickyViewHelper;

public class ViscosityControlActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viscosity_control);

        //拖动效果，测试失败，在小米上没有拖动动画
        StickyFlagView stickyFlagView = (StickyFlagView) findViewById(R.id.sticky_view);
        stickyFlagView.setOnFlagDisappearListener(new StickyFlagView.OnFlagDisappearListener() {
            @Override
            public void onFlagDisappear(StickyFlagView view) {
                Toast.makeText(ViscosityControlActivity.this, "Flag have disappeared.", Toast.LENGTH_SHORT).show();
            }
        });
        stickyFlagView.setFlagText("6");

        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rlay_content);
        StickyFlagView sfv = new StickyFlagView(this);
        sfv.setFlagText("9");
        sfv.setFlagTextColor(Color.WHITE);
        sfv.setFlagTextSize(ScreenUtils.spTopx(this, 15));
        sfv.setFlagColor(Color.BLUE);
        sfv.setFlagRadius(ScreenUtils.dpToPx(this, 10));
        sfv.setMinStickRadius(ScreenUtils.dpToPx(this, 3));
        sfv.setMaxStickRadius(ScreenUtils.dpToPx(this, 8));
        sfv.setMaxDistance(ScreenUtils.dpToPx(this, 80));
//                sfv.setFlagDrawable(R.drawable.bubble);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                (int) ScreenUtils.dpToPx(this, 40),
                (int) ScreenUtils.dpToPx(this, 40));
        lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);


        relativeLayout.addView(sfv);
        sfv.setOnFlagDisappearListener(new StickyFlagView.OnFlagDisappearListener() {
            @Override
            public void onFlagDisappear(StickyFlagView view) {

            }
        });


        TextView mDragView = (TextView) findViewById(R.id.mDragView);

        StickyViewHelper stickyViewHelper = new StickyViewHelper(this, mDragView, R.layout.includeview);
        setViewOut2InRangeUp(stickyViewHelper);
        setViewOutRangeUp(1, stickyViewHelper);
        setViewInRangeUp(stickyViewHelper);
        setViewInRangeMove(stickyViewHelper);
        setViewOutRangeMove(stickyViewHelper);
    }


    /**
     * view在范围外移动执行此Runnable
     *
     * @param stickyViewHelper
     */
    private void setViewOutRangeMove(StickyViewHelper stickyViewHelper) {
        stickyViewHelper.setViewOutRangeMoveRun(new Runnable() {
            @Override
            public void run() {
                DisplayUtils.showToast(ViscosityControlActivity.this, "ViewOutRangeMove");
            }
        });
    }

    /**
     * view在范围内移动指此此Runnable
     *
     * @param stickyViewHelper
     */
    private void setViewInRangeMove(StickyViewHelper stickyViewHelper) {
        stickyViewHelper.setViewInRangeMoveRun(new Runnable() {
            @Override
            public void run() {
                DisplayUtils.showToast(ViscosityControlActivity.this, "ViewInRangeMove");
            }
        });
    }

    /**
     * view没有移出过范围，在范围内松手
     *
     * @param stickyViewHelper
     */
    private void setViewInRangeUp(StickyViewHelper stickyViewHelper) {
        stickyViewHelper.setViewInRangeUpRun(new Runnable() {
            @Override
            public void run() {
                DisplayUtils.showToast(ViscosityControlActivity.this, "ViewInRangeUp");
            }
        });
    }

    /**
     * view移出范围，最后在范围外松手
     *
     * @param position
     * @param stickyViewHelper
     */
    private void setViewOutRangeUp(final int position, StickyViewHelper stickyViewHelper) {
        stickyViewHelper.setViewOutRangeUpRun(new Runnable() {
            @Override
            public void run() {
                DisplayUtils.showToast(ViscosityControlActivity.this, "ViewOutRangeUp");
//                removeList.add(position);
            }
        });
    }

    /**
     * view移出过范围，最后在范围内松手执行次Runnable
     *
     * @param stickyViewHelper
     */
    private void setViewOut2InRangeUp(StickyViewHelper stickyViewHelper) {
        stickyViewHelper.setViewOut2InRangeUpRun(new Runnable() {
            @Override
            public void run() {
                DisplayUtils.showToast(ViscosityControlActivity.this, "ViewOut2InRangeUp");
            }
        });
    }
}
